
(function($, win, doc){
	
	
	/**
	 *  默认值：/seeyon
	 */
	var appBasePath = (function(){
		
		var path = location.pathname;
		
		return path.substring(0, path.indexOf('/', 1));
	})();
		
	var jsonCodeMirror = (function (){

		var editor = null;
		var content = null;

		return {
			init: function(){
				
				setTimeout(function(){
					
					if(null != editor){
						return;
					}
					
					if(content){
						$.setEleValue('#restful_body', content);
					}
					
					editor = CodeMirror.fromTextArea($.getElement('#restful_body'), {
						mode: 'application/json',
						theme: "darcula",
						lineNumbers: true,
						lineWrapping: true
					});
					
					editor.refresh();
					
				}, 200);
			},
			setValue: function(_content){
				
				content = _content;
				
				if(null == editor){
					return ;
				}
				
				editor.setValue(content);
			},
			getValue: function(){
				return (editor ? editor.getValue() : content);
			}
		}
	})();
	
	var app = new Vue({
		el: '.http-test',
		data: {
			style:{
				httpRequestTab_1:{
					'http-request-tab-selected': true
				},
				httpRequestTab_2:{
					'http-request-tab-selected': false
				}
			},
			basic:{
				code: (function(){
					
					var code = $.request.getParament('code');
					
					if($.isBlank(code)){
						return null;
					}
					
					return code;
				})(),
				method: (function(){
					
					var code = $.request.getParament('code');
					
					if($.isBlank(code)){
						return 'GET';
					}
					
					var method = localStorage.getItem(code +  '_DAPI_METHOD');
					
					if($.isBlank(method)){
						return 'GET';
					}
					
					return method;
				})(),
				url: (function(){
					
					var type = $.request.getParament('type');
					var code = $.request.getParament('code');
					
					var url = null;
					
					if(!$.isBlank(code)){
						
						url = localStorage.getItem(code +  '_DAPI_URL');
						
						if(!$.isBlank(url)){
							return url;
						}
					}
					
					url = (appBasePath + '/rest/snail');
					
					if('sql' == type){
						
						url += '/db/' + code;
						
					}else if('groovy' == type){
						
						url += '/x/' + code;
						
					}else{
						return null;
					}
					
					return url;
				})()
			},
			headers:[
				{'key': 'Content-Type', value: 'application/json'},
				{'key': 'Accept', value: 'application/json'}
			]
		},
		methods:{
			onHttpRequestTabSelect: function(index){
				
				var _this = this;
				
				for(var i=1; i<3; i++){
					
					if(index == i){
						
						this.style['httpRequestTab_' + i]['http-request-tab-selected'] = true;
						
						if(2 == index){

							jsonCodeMirror.setValue((function(){
								
								if($.isBlank(_this.basic.code)){
									return '';
								}
								
								var data = localStorage.getItem(_this.basic.code +  '_DAPI_BODY_TEST_DATA');
								
								if($.isBlank(data)){
									return '';
								}
								
								return data;
							})());

							jsonCodeMirror.init();
						}
					}else{
						this.style['httpRequestTab_' + i]['http-request-tab-selected'] = false;
					}
				}
			},
			onHttpRequestAddHeader: function(event){
				
				var isCan = true;
				
				for(var i=0,len=this.headers.length; i<len; i++){
					
					if($.isBlank(this.headers[i].key)){
						isCan = false;
						break;
					}
				}
				
				if(isCan){
					this.headers.push({'key': '', value: ''});
				}
			},
			onSend(event){
				
				var headers = this.headers;
				var data = (function(){
					
					var data = jsonCodeMirror.getValue();
					
					if($.isBlank(data)){
						return null;
					}
					
					return data;
				})();

				var loading = this.$loading({lock: true, text: 'Loading', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.8)'});
				
				$.http.ajax({
					url: this.basic.url,
					method: this.basic.method,
					header:(function(){
						
						var h = {};
						
						for(var i=0,len=headers.length; i<len; i++){
							
							if($.isBlank(headers[i].key)){
								continue;
							}
							
							h[headers[i].key] = headers[i].value;
						}
						
						return h;
					})(),
					data: data,
					onsuccess: function(data, status, xhr){
						$.setEleValue('#restful_result', ($.isObject(data) ? $.toJSONString(data) : data));
					}, 
					onerror: function(responseText, status, xhr){
						$.setEleValue('#restful_result', responseText);
					},
					oncomplete: function(responseText, status, xhr){
						loading.close();
					}
				});

				if(null != this.basic.code){
					
					localStorage.setItem(this.basic.code +  '_DAPI_URL', this.basic.url);
					localStorage.setItem(this.basic.code +  '_DAPI_METHOD', this.basic.method);
					
					if(null != data){
						localStorage.setItem(this.basic.code +  '_DAPI_BODY_TEST_DATA', data);
					}
				}
			}
		}
	});
		
})(Snail, window, document);


